﻿package psk.lightshow;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
	private Activity nowActivity = MainActivity.this;
	private TextView tvBgText,tvBgPhoneAll,tvBgPhoneDot,tvBgPhonePattern;
	private int position=0,r=255,g=255,b=255;
	private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.home);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.head_title);
        initHead();
        initView();
    }
    private void initHead(){
    	TextView tvTitle = (TextView)findViewById(R.id.tvTitle);
        tvTitle.setText("光涂");
    }
    private void initView(){
    	tvBgText         = (TextView)findViewById(R.id.tvBgText);
    	tvBgPhoneAll     = (TextView)findViewById(R.id.tvBgPhoneAll);
    	tvBgPhoneDot     = (TextView)findViewById(R.id.tvBgPhoneDot);
    	tvBgPhonePattern = (TextView)findViewById(R.id.tvBgPhonePattern);
    	BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(R.drawable.icon_color_long);
    	bitmap = bd.getBitmap();
    	int w = bitmap.getWidth();
    	changeColor(0);
    	SeekBar sbChangeColor = (SeekBar)findViewById(R.id.sbChangeColor);
    	sbChangeColor.setMax(w-1);
    	sbChangeColor.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				position = progress;
				changeColor(progress);
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
    		
    	});
    	Button btnText         = (Button)findViewById(R.id.btnText);
    	Button btnPhoneAll     = (Button)findViewById(R.id.btnPhoneAll);
    	Button btnPhoneDot     = (Button)findViewById(R.id.btnPhoneDot);
    	Button btnDrawDesign   = (Button)findViewById(R.id.btnDrawDesign);
    	Button btnPhonePattern = (Button)findViewById(R.id.btnPhonePattern);
    	Button btnToFlashLight = (Button)findViewById(R.id.btnToFlashLight);
    	btnText.setOnClickListener(new btnListener());
    	btnPhoneAll.setOnClickListener(new btnListener());
    	btnPhoneDot.setOnClickListener(new btnListener());
    	btnDrawDesign.setOnClickListener(new btnListener());
    	btnPhonePattern.setOnClickListener(new btnListener());
    	btnToFlashLight.setOnClickListener(new btnListener());
    }

    private void changeColor(int progress){
    	int pixel = bitmap.getPixel(progress, 5);
		r = Color.red(pixel);
		g = Color.green(pixel);
		b = Color.blue(pixel);
		tvBgText.setTextColor(Color.rgb(r, g, b));
		tvBgPhoneAll.setBackgroundColor(Color.rgb(r, g, b));
		tvBgPhoneDot.setBackgroundColor(Color.rgb(r, g, b));
		tvBgPhonePattern.setBackgroundColor(Color.rgb(r, g, b));
    }
    private class btnListener implements OnClickListener{
    	public void onClick(View v){
    		Intent it;
    		Bundle bl;
    		switch(v.getId()){
    		case R.id.btnText:
    			it = new Intent(nowActivity,ModelText.class);
    			bl = new Bundle();
    			bl.putInt("position", position);
    			it.putExtras(bl);
    			startActivity(it);
    			break;
    		case R.id.btnPhoneDot:
    			it = new Intent(nowActivity,ModelDot.class);
    			bl = new Bundle();
    			bl.putInt("position", position);
    			it.putExtras(bl);
    			startActivity(it);
    			break;
    		case R.id.btnPhoneAll:
    			it = new Intent(nowActivity,Show.class);
    			bl = new Bundle();
    			bl.putString("type", "full");
    			bl.putInt("r", r);
    			bl.putInt("g", g);
    			bl.putInt("b", b);
    			it.putExtras(bl);
    			startActivity(it);
    			break;
    		case R.id.btnToFlashLight:
    			it = new Intent(nowActivity,Flash.class);
    			startActivity(it);
    		}
    	}
    }
}
